package com.abso.sunlight.explorer.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.abso.sunlight.explorer.LegislatorSearch;
import com.abso.sunlight.explorer.dialogs.LegislatorSearchDialog;
import com.abso.sunlight.explorer.jobs.RunLegislatorSearchJob;

/**
 * Creates a new search.
 */
public class NewLegislatorSearchHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        Shell shell = HandlerUtil.getActiveShell(event);
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        LegislatorSearchDialog dialog = new LegislatorSearchDialog(shell, null);
        if (dialog.open() == Window.OK) { // Run
            LegislatorSearch search = new LegislatorSearch();
            search.setName(dialog.getSearchName());
            search.setQuery(dialog.getLegislatorQuery());
            new RunLegislatorSearchJob(window, search).schedule();
        }
        return null;
    }

}
